﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace FileEncrypt.Encrypt
{
    public class DESEncrypt:EntryptBase,FileEncrypt.IEncrypt.IEncrypt
    {
        public Boolean DecryptFile(string inFileName, string outFileName, string Key, string IV)
        {
            byte[] myKey = GetKey(Key);
            byte[] myIV = GetIV(IV);

            byte[] buffer = new byte[bufferSize];

            using (AesCryptoServiceProvider encrypted = new AesCryptoServiceProvider())
            {
                using (ICryptoTransform decryptor = encrypted.CreateDecryptor(myKey, myIV))
                {
                    using (FileStream fsRead = new FileStream(inFileName, FileMode.Open, FileAccess.Read))
                    {
                        using (FileStream fsOutPut = new FileStream(outFileName, FileMode.Create, FileAccess.ReadWrite))
                        {
                            try
                            {
                                using (CryptoStream cs = new CryptoStream(fsOutPut, decryptor, CryptoStreamMode.Write))
                                {
                                    try
                                    {
                                        int length = 0;
                                        while ((length = fsRead.Read(buffer, 0, bufferSize)) > 0)
                                        {
                                            cs.Write(buffer, 0, (int)length);
                                        }
                                    }
                                    catch
                                    {
                                        return false;
                                    }
                                    return true;
                                }
                            }
                            catch
                            {
                                return false;
                            }
                        }
                    }
                }
            }
        }
        public Boolean EncryptFile(string inFileName, string outFileName, string Key, string IV)
        {
            byte[] buffer = new byte[bufferSize];

            byte[] myKey = GetKey(Key);
            byte[] myIV = GetIV(IV);

            using (AesCryptoServiceProvider encrypted = new AesCryptoServiceProvider())
            {
                using (ICryptoTransform encryptor = encrypted.CreateEncryptor(myKey, myIV))
                {
                    using (FileStream fsRead = new FileStream(inFileName, FileMode.Open, FileAccess.Read))
                    {
                        using (FileStream fsOutPut = new FileStream(outFileName, FileMode.Create, FileAccess.ReadWrite))
                        {
                            using (CryptoStream cs = new CryptoStream(fsOutPut, encryptor, CryptoStreamMode.Write))
                            {
                                try
                                {
                                    int length = 0;
                                    while ((length = fsRead.Read(buffer, 0, bufferSize)) > 0)
                                    {
                                        cs.Write(buffer, 0, (int)length);
                                    }
                                }
                                catch
                                {
                                    return false;
                                }
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
}
